# -*- coding: utf-8 -*-
# from __future__ import unicode_literals

import sys
from PySide6.QtGui import QFont, QColor
from PySide6.QtWidgets import (QWidget,
                               QHeaderView,
                               QTabWidget,
                               QApplication, QTableWidget, QTableWidgetItem)
from PySide6.QtCore import Qt
from tab_1 import Tab1
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)

class MainWindow(QTabWidget):
    def __init__(self):
        super().__init__()
        self.resize(820, 640)
        self.setTabPosition(QTabWidget.East)
        self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
        self.setAcceptDrops(True)

        self.font = QFont()
        self.font.setFamily("Arial")
        self.font.setPointSize(14)
        self.setFont(self.font)

        self.tab_1 = Tab1()
        self.tb4 = QTableWidget()
        self.tb5 = QWidget()
        self.tb6 = QTableWidget()
        header = self.tb6.horizontalHeader()
        header_ = self.tb4.horizontalHeader()
        header.setStretchLastSection(True)
        header_.setStretchLastSection(True)
        header.setSectionResizeMode(QHeaderView.Stretch)
        header_.setSectionResizeMode(QHeaderView.Stretch)
        self.tb6.resizeRowsToContents()
        self.tb4.resizeRowsToContents()
        self.tb6.resizeColumnsToContents()
        self.tb4.resizeColumnsToContents()
        self.addTab(self.tab_1, "Documents")
        self.tab_1.deleteandaddtab.connect(self.deletewidget)
        self.tab_1.tab5addly5.connect(self.tab5)
        self.tab_1.lyt4.connect(self.lyt4)
        self.tab_1.widget.itemDoubleClicked.connect(self.fillt6)

    def deletewidget(self):
        self.tb5.setLayout(self.tab_1.lt5)
        self.removeTab(3)
        self.removeTab(2)
        self.removeTab(1)

    def tab5(self):
        self.tb5.setLayout(self.tab_1.lt5)
        self.addTab(self.tb5, 'rank')

    def fillt6(self, item):
        print("show condence words")
        name = item.text()
        name_ = name.split('.')[0]
        clt = self.tab_1.fc.condence[name_]
        le = len(clt)
        le1 = len(clt[0])
        self.tb6.setRowCount(le)
        self.tb6.setColumnCount(le1)
        for i in range(le):
            for j in range(le1):
                qtwi = QTableWidgetItem(str(clt[i][j]))
                self.tb6.setItem(i, j, qtwi)

        self.addTab(self.tb6, 'Condence words')
        self.setCurrentWidget(self.tb6)

    def lyt4(self, chart, wl_):
        ifshowtb4 = False
        chtle = len(chart)
        self.tb4.setRowCount(chtle)
        self.tb4.setColumnCount(chtle)
        for i in range(chtle):
            for j in range(chtle):
                if i == 0 and j != 0:
                    tblwi = QTableWidgetItem(wl_[j - 1])
                    self.tb4.setItem(i, j, tblwi)
                elif j == 0 and i != 0:
                    tblwi = QTableWidgetItem(wl_[i - 1])
                    self.tb4.setItem(i, j, tblwi)
                elif i != j and chart[i][j] > 0.5:
                    ifshowtb4 = True
                    rast = str(chart[i][j])
                    tblwi = QTableWidgetItem(rast)
                    green = QColor("green")
                    tblwi.setBackground(green)
                    self.tb4.setItem(i, j, tblwi)
        if ifshowtb4:
            print("show chart")
            self.addTab(self.tb4, 'chart')

    def dragEnterEvent(self, a0):
        if a0.mimeData().hasUrls():
            a0.acceptProposedAction()
            self.tab_1.setcurrentwidget()
        else:
            print("NULL")

    def dropEvent(self, a0):
        for url in a0.mimeData().urls():
            path = url.toLocalFile()
            if path not in self.tab_1.wordlist:
                self.tab_1.getpath(path)
            else:
                print("already get this file")

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.RightButton:
            clipb = QApplication.clipboard()
            mia = clipb.mimeData()
            if mia.hasUrls():
                self.tab_1.miahasurls(mia)
            else:
                print("NULL")

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Backspace:
            self.tab_1.deletePressed()
        elif event.key() == Qt.Key_Return:
            self.tab_1.enterPressed()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dui = MainWindow()
    dui.show()
    sys.exit(app.exec_())

# Form implementation generated from reading ui file 'main.py'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
